// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * LZO decompressor for the Linux kernel. Code borrowed from the lzo
 * implementation by Markus Franz Xaver Johannes Oberhumer.
 *
 * Linux kernel adaptation:
 * Copyright (C) 2009
 * Albin Tonnerre, Free Electrons <albin.tonnerre@free-electrons.com>
 *
 * Original code:
 * Copyright (C) 1996-2005 Markus Franz Xaver Johannes Oberhumer
 * All Rights Reserved.
 *
 * Markus F.X.J. Oberhumer
 * <markus@oberhumer.com>
 * http://www.oberhumer.com/opensource/lzop/
 */

#include <stdint.h>
#include <string.h>
#include "lzo.h"

static const uint8_t lzop_magic[] = {
	0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a };

#define LZO_BLOCK_SIZE        (256*1024l)
#define HEADER_HAS_FILTER      0x00000800L
#define HEADER_SIZE_MIN       (9 + 7     + 4 + 8     + 1       + 4)
#define HEADER_SIZE_MAX       (9 + 7 + 1 + 8 + 8 + 4 + 1 + 255 + 4)

static inline long parse_header(uint8_t *input, long *skip, long in_len)
{
	int l;
	uint8_t *parse = input;
	uint8_t *end = input + in_len;
	uint8_t level = 0;
	uint16_t version;

	/*
	 * Check that there's enough input to possibly have a valid header.
	 * Then it is possible to parse several fields until the minimum
	 * size may have been used.
	 */
	if (in_len < HEADER_SIZE_MIN)
		return 0;

	/* read magic: 9 first bits */
	for (l = 0; l < 9; l++) {
		if (*parse++ != lzop_magic[l])
			return 0;
	}
	/* get version (2bytes), skip library version (2),
	 * 'need to be extracted' version (2) and
	 * method (1) */
	version = GET_UINT16_BE(parse, 0);
	parse += 7;
	if (version >= 0x0940)
		level = *parse++;
	if (GET_UINT32_BE(parse, 0) & HEADER_HAS_FILTER)
		parse += 8; /* flags + filter info */
	else
		parse += 4; /* flags */

	/*
	 * At least mode, mtime_low, filename length, and checksum must
	 * be left to be parsed. If also mtime_high is present, it's OK
	 * because the next input buffer check is after reading the
	 * filename length.
	 */
	if (end - parse < 8 + 1 + 4)
		return 0;

	/* skip mode and mtime_low */
	parse += 8;
	if (version >= 0x0940)
		parse += 4;	/* skip mtime_high */

	l = *parse++;
	/* don't care about the file name, and skip checksum */
	if (end - parse < l + 4)
		return 0;
	parse += l + 4;

	*skip = parse - input;
	return 1;
}

int unlzo(uint8_t *input, long in_len, uint8_t *output)
{
	uint8_t r = 0;
	long skip = 0;
	uint32_t src_len, dst_len;
	size_t tmp;
	uint8_t *in_buf, *in_buf_save, *out_buf;
	int ret = -1;

	if (output) {
		out_buf = output;
	}

	if (input) {
		in_buf = input;
	}
	in_buf_save = in_buf;

	if (!parse_header(in_buf, &skip, in_len)) {
		// error("invalid header");
		goto exit_2;
	}
	in_buf += skip;
	in_len -= skip;


	for (;;) {
		/* read uncompressed block size */
		if (in_len < 4) {
			// error("file corrupted");
			goto exit_2;
		}
		dst_len = GET_UINT32_BE(in_buf, 0);
		in_buf += 4;
		in_len -= 4;

		/* exit if last block */
		if (dst_len == 0) {
			break;
		}

		if (dst_len > LZO_BLOCK_SIZE) {
			// error("dest len longer than block size");
			goto exit_2;
		}

		/* read compressed block size, and skip block checksum info */
		if (in_len < 8) {
			// error("file corrupted");
			goto exit_2;
		}
		src_len = GET_UINT32_BE(in_buf, 0);
		in_buf += 8;
		in_len -= 8;

		if (src_len <= 0 || src_len > dst_len) {
			// error("file corrupted");
			goto exit_2;
		}

		/* decompress */
		if (in_len < src_len) {
			// error("file corrupted");
			goto exit_2;
		}
		tmp = dst_len;

		/* When the input data is not compressed at all,
		 * lzo1x_decompress_safe will fail, so call memcpy()
		 * instead */
		if (unlikely(dst_len == src_len))
			memcpy(out_buf, in_buf, src_len);
		else {
			r = lzo1x_decompress_safe((uint8_t *) in_buf, src_len,
						out_buf, &tmp);

			if (r != LZO_E_OK || dst_len != tmp) {
				// error("Compressed data violation");
				goto exit_2;
			}
		}

		if (output)
			out_buf += dst_len;

		in_buf += src_len;
		in_len -= src_len;
	}

	ret = 0;
exit_2:
exit_1:
exit:
	return ret;
}
